package cn.minds.capture.dispatch.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import cn.minds.capture.dispatch.task.TaskHandlerFacotry;

public class Server {
	private int port;
	private TaskHandlerFacotry taskHandlerFacotry;

	public Server(TaskHandlerFacotry taskHandlerFacotry, int port) {
		this.port = port;
		this.taskHandlerFacotry = taskHandlerFacotry;
	}

	public void start() {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(port);
			while (true) {
				Socket socket = serverSocket.accept();
				new HandlerServer(WorkerManager.createTaskHandler(taskHandlerFacotry), socket).start();
			}
		} catch (IOException e) {
			try {
				serverSocket.close();
			} catch (IOException e1) {
			}
			e.printStackTrace();
		}
	}
}
